package nl.stroep.classbuilder.cb 
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import nl.stroep.classbuilder.cb.interfaces.ICbObject;
	import nl.stroep.classbuilder.cb.interfaces.ICbWrapper;
	import nl.stroep.classbuilder.definitions.Chars;
	import nl.stroep.classbuilder.definitions.ClassMethods;
	import nl.stroep.classbuilder.definitions.DataTypes;
	import nl.stroep.classbuilder.utils.ObjectUtil;
	import nl.stroep.classbuilder.utils.XMLUtil;
	
	/**
	 * Specifies a variable definition.
	 * @author Mark Knol - Stroep.nl - 2010 (c)
	 */
	public class CbVariable	extends EventDispatcher implements ICbObject, ICbWrapper, IEventDispatcher
	{
		private var cbObject:CbObject;
		
		private var _name:String;
		private var _type:String;
		private var _accessModifier:String;
		private var _modifier:String;
		private var _defaultValue:String;
		private var _isGetter:Boolean;
		private var _isSetter:Boolean;
		
		public var isConstant:Boolean;
		/**
		 * 
		 * @param	name				Required. 
		 * @param	type				Required. 
		 * @param	defaultValue		Optional. 
		 * @param	accessModifier		Optional. 
		 * @param	modifier			Optional. Use statics from nl.stroep.classbuilder.definitions.Variable
		 * @param	isConstant			Optional. Variable is a constant value
		 * @param	isGetter			Optional. Variable will be defined as gettter
		 * @param	isSetter			Optional. Variable will be defined as setter
		 */
		public function CbVariable( name:String, type:String, defaultValue:String = null, accessModifier:String = "public", modifier:String = null, isConstant:Boolean = false, isGetter:Boolean = false, isSetter:Boolean = false)
		{
			cbObject = new CbObject( isConstant ? ClassMethods.CONST : ClassMethods.VAR, name);
			
			this._name = name;
			this._type = type;
			this._defaultValue = defaultValue;
			this._accessModifier = accessModifier;
			this._modifier = modifier;
			this.isConstant = isConstant;
			this._isGetter = isGetter;
			this._isSetter = isSetter;
		}
		
		public override function toString():String
		{
			cbObject.name = (this._isGetter || this._isSetter) ? "_" + this._name : this._name;
			cbObject.type = this._type;
			cbObject.accessModifier = this._accessModifier;
			cbObject.modifier = this._modifier;
			cbObject.classMethod = isConstant ? ClassMethods.CONST : ClassMethods.VAR;
			cbObject.defaultValue = (this._defaultValue ? Chars.EQUAL + (this._type == DataTypes.STRING ? Chars.QUOTE + this._defaultValue + Chars.QUOTE : this._defaultValue ) + Chars.SEMICOLON : Chars.SEMICOLON);
			
			return cbObject.toString();
		}
		
		public function toXML():XML
		{
			var xml:XML = <variable/>;
			
			xml.appendChild( XMLUtil.getNode("name", this._name) );
			xml.appendChild( XMLUtil.getNode("type", this._type) );
			xml.appendChild( XMLUtil.getNode("defaultValue",this._defaultValue) );
			xml.appendChild( XMLUtil.getNode("accessModifier", this._accessModifier) );
			xml.appendChild( XMLUtil.getNode("modifier", this._modifier) );
			xml.appendChild( XMLUtil.getNode("isConstant", isConstant) );
			xml.appendChild( XMLUtil.getNode("isGetter", this._isGetter) );
			xml.appendChild( XMLUtil.getNode("isSetter", this._isSetter) );
			
			return xml;
		}
		
		public static function parseItem(xml:XML):CbVariable
		{
			var variable:CbVariable = new CbVariable(
				ObjectUtil.parseString( xml.name.text() ),
				ObjectUtil.parseString( xml.type.text() ),
				ObjectUtil.parseString( xml.defaultValue.text() ),
				ObjectUtil.parseString( xml.accessModifier.text() ),
				ObjectUtil.parseString( xml.modifier.text() ),
				ObjectUtil.parseBoolean( xml.isConstant.text() ),
				ObjectUtil.parseBoolean( xml.isGetter.text() ),
				ObjectUtil.parseBoolean( xml.isSetter.text() )
			)
			return variable;
		}
		
		public static function parseData(xml:XMLList):Vector.<CbVariable> 
		{
			var retval:Vector.<CbVariable> = new Vector.<CbVariable>();
			
			if (xml.variable.length() > 0)
			{
				const total:uint = xml.variable.length();
				
				for (var i:int = 0; i < total; ++i) 
				{
					var variable:CbVariable = CbVariable.parseItem(xml.variable[i]);
					retval.push(variable);
				}
			}
			
			return retval;
		}
		
		public function get name():String { return _name; }
		
		public function set name(value:String):void 
		{
			_name = value;
		}
		
		public function get type():String { return _type; }
		
		public function set type(value:String):void 
		{
			_type = value;
		}
		
		public function get accessModifier():String { return _accessModifier; }
		
		public function set accessModifier(value:String):void 
		{
			_accessModifier = value;
		}
		
		public function get modifier():String { return _modifier; }
		
		public function set modifier(value:String):void 
		{
			_modifier = value;
		}
		
		public function get defaultValue():String { return _defaultValue; }
		
		public function set defaultValue(value:String):void 
		{
			_defaultValue = value;
		}
		
		public function get isGetter():Boolean { return _isGetter; }
		
		public function set isGetter(value:Boolean):void 
		{
			_isGetter = value;
		}
		
		public function get isSetter():Boolean { return _isSetter; }
		
		public function set isSetter(value:Boolean):void 
		{
			_isSetter = value;
		}
		
		
	}

}